using System;
using System.Collections.Generic;
using System.Text;

namespace k.Frame.Utility
{
    public class InstanceCreator
    {
        static InstanceCreator()
        {
            //string[] arr=System.IO.Directory.GetFiles(System.Environment.CurrentDirectory);
            //for (int i = 0; i < arr.Length; i++)
            //{
            //    if (arr[i].EndsWith(".dll"))
            //    {
            //        string[] arr1 = arr[i].Split('\\');
            //        if (arr1[arr1.Length - 1].StartsWith("f"))
            //        {
            //            System.Reflection.Assembly asm = System.Reflection.Assembly.LoadFile(arr[i]);
            //            m_AssemblyContainer.Add(arr[i], asm);
            //            Type[] arr2 = asm.GetTypes();
            //            for (int j = 0; j < arr2.Length; j++)
            //            {
            //                m_ClassContainer.Add(arr[i] + "_" + arr2[j].FullName, arr2[j]);
            //            }
            //        }
            //    }
            //}
        }
        private static System.Collections.Generic.SortedList<string, System.Reflection.Assembly> m_AssemblyContainer = new System.Collections.Generic.SortedList<string, System.Reflection.Assembly>();
        private static System.Collections.Generic.SortedList<string, System.Type> m_ClassContainer = new System.Collections.Generic.SortedList<string, System.Type>();
        public static object GetInstance(string dllName, string className)
        {
            return getClass(dllName, className);
        }
        internal static System.Reflection.Assembly getAssembly(string dllName)
        {
            if (m_AssemblyContainer.ContainsKey(dllName))
            {
                return m_AssemblyContainer[dllName];
            }
            else
            {
                if (!System.IO.File.Exists(dllName))
                {
                    throw new System.IO.FileNotFoundException("The assembly could not be found", dllName);
                }

                System.Reflection.Assembly assembly = null;
                lock (m_AssemblyContainer)
                {
                    assembly = System.Reflection.Assembly.LoadFile(dllName);
                    if (m_AssemblyContainer.ContainsKey(dllName)) return m_AssemblyContainer[dllName];
                    m_AssemblyContainer.Add(dllName, assembly);
                }
                return assembly;
            }
        }
        internal static Type getType(string dllName, string className)
        {
            if (m_ClassContainer.ContainsKey(dllName + "_" + className))
            {
                return m_ClassContainer[dllName + "_" + className];
            }
            else
            {
                System.Reflection.Assembly asm = getAssembly(dllName);
                Type retval = asm.GetType(className);
                lock (m_ClassContainer)
                {
                    if (m_ClassContainer.ContainsKey(dllName + "_" + className)) return m_ClassContainer[dllName + "_" + className];
                    m_ClassContainer.Add(dllName + "_" + className, retval);
                }
                return retval;
            }
        }
        private static object getClass(string dllName, string className)
        {
            if (m_ClassContainer.ContainsKey(dllName + "_" + className))
            {
                return m_ClassContainer[dllName + "_" + className].Assembly.CreateInstance(className);
            }
            else
            {
                Type t = getType(dllName, className);
                return t.Assembly.CreateInstance(className);
            }
        }


    }
}
